package zheng.mockit.plugin.http.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zcz on 16-7-30.
 */
public class HttpParamUtils {

    public static Map<String, String> getParamMap(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String paramKey = parameterNames.nextElement();
            params.put(paramKey, request.getParameter(paramKey));
        }
        return params;
    }

    public static String getParamQuery(Map<String, String> params) {
        StringBuffer queryBuffer = new StringBuffer();
        for (String pk : params.keySet()) {
            queryBuffer.append(pk).append("=").append(params.get(pk)).append("&");
        }
        queryBuffer.delete(queryBuffer.length() - 1, queryBuffer.length());
        return queryBuffer.toString();
    }
}
